package com.saifan.wyy_ov.ui.onlishop;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.common.BaseActivity;
import com.saifan.wyy_ov.config.Api;
import com.saifan.wyy_ov.data.bean.StoreCategoryBean;
import com.saifan.wyy_ov.data.bean.StoreCategoryBean.ChildStoreType;
import com.saifan.wyy_ov.model.impl.GetData;
import com.saifan.wyy_ov.model.interfaces.IGetData;
import com.saifan.wyy_ov.model.interfaces.ResponseData;
import com.saifan.wyy_ov.ui.view.NoScrollGridView;
import com.saifan.wyy_ov.utils.CommonAdapter;
import com.saifan.wyy_ov.utils.ViewHolderHelper;

/**
 * @author WangLi
 * @date 2015年2月27日 下午1:39:06
 * @Description 店铺类别页面
 * @version V1.0
 */

public class StoreCategoryActivity extends BaseActivity {
	private IGetData getData;
	private ListView categoryListView;
	private List<StoreCategoryBean> list;
	private CommonAdapter<StoreCategoryBean> listAdapter;
	private RelativeLayout loadingLayout;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_store_category);
		list = new ArrayList<StoreCategoryBean>();
		findViews();
        setEnabledNavigation(true);
		getData();

	}

	private void getData() {

		loadingLayout.setVisibility(View.VISIBLE);
		getData = new GetData();
		getData.httpGetDataJson(StoreCategoryActivity.this, Api.StoreList.GETSTORETYPE, "", new ResponseData() {

			@Override
			public void onSuccess(String json) {
				loadingLayout.setVisibility(View.GONE);
				Gson gson = new Gson();
				list = gson.fromJson(json, new TypeToken<List<StoreCategoryBean>>() {
				}.getType());
				for (int i = 0; i < list.size(); i++) {
					for (int j = 0; j < list.get(i).getChildStoreType().size(); j++) {
						list.get(i).getChildStoreType().get(j).setSelect(false);
					}
				}
				listAdapter = new CommonAdapter<StoreCategoryBean>(StoreCategoryActivity.this, list, R.layout.store_category_item) {

					@Override
					public void convert(ViewHolderHelper helper, final StoreCategoryBean item) {
						helper.setText(R.id.store_category_title, item.getTypeName());
						NoScrollGridView gridView = helper.getView(R.id.store_category_grid);
						final CommonAdapter<ChildStoreType> gridAdapter = new CommonAdapter<ChildStoreType>(
								StoreCategoryActivity.this, item.getChildStoreType(), R.layout.store_category_grid_item) {

							@SuppressLint({ "NewApi", "ResourceAsColor" })
							@Override
							public void convert(ViewHolderHelper helper, ChildStoreType item) {
								helper.setText(R.id.store_category_grid_title, item.getTypeName());
								if (item.isSelect()) {
									TextView titleTextView = helper.getView(R.id.store_category_grid_title);
									Drawable drawable = getResources().getDrawable(R.drawable.store_category_grid_item_corner);
									titleTextView.setBackground(drawable);
//									titleTextView.setTextColor(getResources().getColor(R.color.white));
								} else {
									TextView titleTextView = helper.getView(R.id.store_category_grid_title);
									titleTextView.setBackgroundColor(getResources().getColor(R.color.alpha));
//									titleTextView.setTextColor(getResources().getColor(R.color.white));
								}

							}
						};
						gridView.setAdapter(gridAdapter);
						gridView.setOnItemClickListener(new OnItemClickListener() {

							@Override
							public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
								Intent intent = new Intent(StoreCategoryActivity.this, AroundMallActivity.class);
								intent.putExtra("category", item.getChildStoreType().get(arg2).getTypeName());
								startActivity(intent);
							}
						});
					}
				};
				categoryListView.setAdapter(listAdapter);
			}

			@Override
			public void onFailed(String str) {

			}
		});
	}

	private void findViews() {
		categoryListView = (ListView) findViewById(R.id.category_list);
		loadingLayout = (RelativeLayout) findViewById(R.id.loading_lay);
	}
}
